package com.sl.au.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.FlowBaseInfo;
import com.sl.au.entity.FlowConfigWithBusinessInfo;
import com.sl.au.entity.FlowTotalInfo;
import com.sl.au.entity.User;
import com.sl.au.repository.FlowBaseInfoRepository;
import com.sl.au.repository.FlowConfigWithBusinessInfoRespository;
import com.sl.au.service.FlowTotalInfoService;
import com.sl.au.service.QueryService;

@Service("FlowTotalInfoService")
@Transactional
public class FlowTotalInfoServiceImpl extends BaseServiceImpl implements FlowTotalInfoService {

	@Autowired
	QueryService queryService;
	
	@Autowired
	FlowBaseInfoRepository flowBaseInfoRepository;
	
	@Autowired
	FlowConfigWithBusinessInfoRespository flowConfigWithBusinessInfoRespository;
	//加载树
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getNodeMap() {
		List<Map<String, Object>> nodeMap = new ArrayList<Map<String, Object>>();
		List<FlowBaseInfo> listBaseInfo = this.flowBaseInfoRepository.findAll();
		String flowIds = "";
		if(listBaseInfo == null || listBaseInfo.size() == 0){
			return null;
		}
		for(FlowBaseInfo p1:listBaseInfo){
			flowIds += p1.getnFlowID()+",";
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("parentId", "0");
			map.put("id", p1.getnFlowID() + "_flow");
			map.put("name", p1.getcFlowDes());
			map.put("memo", "流程名称");
			nodeMap.add(map);
		}
		flowIds = flowIds.substring(0, flowIds.length()-1);
		
		StringBuilder hql = new StringBuilder();
		hql.append("select a from FlowConfigWithBusinessInfo a where a.nFlowID in (" +flowIds+ ")");
		Query query=queryService.createQueryHql(hql.toString());
		List<FlowConfigWithBusinessInfo> listFlowConfigWithBusinessInfo = query.getResultList();
		
		for(FlowConfigWithBusinessInfo p2 : listFlowConfigWithBusinessInfo){
			Map<String, Object> map = new HashMap<String, Object>();
			if(p2.getnParentConfigID() == 0){//流程根节点
				map.put("parentId", p2.getnFlowID() + "_flow");
			}else{
				map.put("parentId", p2.getnParentConfigID() + "_node");
			}
			map.put("id", p2.getnFlowConfigID() + "_node");
			map.put("name", p2.getcFlowConfigName());
			map.put("memo", p2.getcFlowConfigDes());
			nodeMap.add(map);
		}
		
		return nodeMap;
	}

	@Override
	public List<FlowTotalInfo> getFlowTotalInfoHql(int start, int length, Map<String, Object> condition, User user,
			String orderName, String dir) {
		@SuppressWarnings("unchecked")
		HashMap<String, Object>numMap=(HashMap<String, Object>)condition.get("numMap");
		Map<String,Object>namedParamsMap=new HashMap<String,Object>();
		StringBuilder hql=queryService.buildCombinedQueryHql("FlowTotalInfo",null, condition, namedParamsMap,null, user);
		
		for(String key:numMap.keySet()){
			Object value=numMap.get(key);
			if(value instanceof String){
				 if(StringUtils.hasText((String)value)){
					 if(key == "cEmpName"){
						 hql.append("and (e.nEmpID."+key+" like '%"+value.toString()+"%')");
					 }else if(key == "cDeptName"){
						 hql.append("and (e.nDeptID."+key+" like '%"+value.toString()+"%')");
					 }else if(key == "id"){
						 String[] a = value.toString().split("_");
						 if(a[1].toString().equals("flow") ){
							hql.append("and (e.nFlowID = ");
						}
						if(a[1].toString().equals("node")){
							hql.append("and (e.nFlowConfigID = ");
						}
						hql.append(Integer.parseInt(a[0]) + ")");
					 }else
						 hql.append("and (e."+key+" like '%"+value.toString()+"%')");
				 }
			} 
		}
		String dataFilter=condition.get("dataFilter").toString();
		
		hql.append(" order by " + orderName + " " + dir);
		Query query=queryService.createQueryHql(hql.toString());
		query.setFirstResult(start).setMaxResults(length);
		return queryService.executeHql(query, namedParamsMap);
	}

}
